package com.demo.rocket.controller;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.exception.RemotingException;
import com.demo.rocket.mq.RocketMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/")
public class IndexController {

    // 注入进来
    @Autowired
    @Qualifier("rocketMQProducer")
    private RocketMQProducer rocketMQProducer;

    @GetMapping("/test")
    public Object test(HttpServletRequest request){
        return "!";
    }


    @PostMapping("/post")
    public Object post(HttpServletRequest request,String msg) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        SendResult send = rocketMQProducer.getDefaultMQProducer().send(new Message("topic_test", "topic_test_tag", "hello".getBytes()));
        System.err.println("send success");
        return send;
    }
}
